/**
 * 1
 *  
 * @Author : roryrywang
 * @DateTime : 24-9-8 下午1:57
 */

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<char>> g(n, vector<char>(m));
    vector<vector<int>> f(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> g[i][j];
            f[i][j] = 0;
        }
    }

    unordered_map<char, pair<int, int>> d = {{'L', {0,  -1}},
                                             {'R', {0,  1}},
                                             {'U', {-1, 0}},
                                             {'D', {1,  0}}};
    unordered_map<int, bool> map;
    int idx = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (!f[i][j]) {
                idx++;
                int x = i;
                int y = j;
                while (x >= 0 && x < n && y >= 0 && y < m && f[x][y] == 0) {
                    f[x][y] = idx;
                    // 注意x y的更新
                    auto dir = d[g[x][y]];
                    x += dir.first;
                    y += dir.second;
                }
                if (x >= 0 && x < n && y >= 0 && y < m) {
                    if (f[x][y] == idx) {
                        map[idx] = true;
                    } else {
                        map[idx] = map[f[x][y]];
                    }
                } else {
                    map[idx] = false;
                }
            }
        }
    }

    int res = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (map[f[i][j]]) res++;
        }
    }

    cout << res;

    return 0;
}